/**
 * Created by caosh on 2018/1/19 0019.
 */
var Cart = new Vue({
    el: '#cart',
    created: function () {
    },
    data: {
        isShowMask: false,
        num: 1,
        product: {},
        isAll: false,
        selected: 'cart',
        tip: '门店名称',
        control: '编辑',
        recommendHeight: 0,
        cartHeight: 0,
        cardContentWidth: 0,
        recommend: {} // 点击的推荐商品
    },
    mixins: [CartMixin, ProductMixin, SystemMixin],
    watch: {
        selected: function (val, oldVal) {
            if (val != oldVal) { // tabbar切换
                if (val == 'all') {
                    window.location.href = '../product/index.html';
                } else if (val == 'cart') {
                    window.location.href = '../cart/index.html';
                } else if (val == 'mine') {
                    window.location.href = '../mine/index.html';
                } else if (val == 'home') {
                    window.location.href = '../index.html';
                }
            }
        },
        cartProducts: {
            handler(newValue, oldValue) {
                this.getCartProductsNum();
                this.getCartProductsTotal();
                this.cacheCartProducts();
                this.showRecommends();
            },
            deep: true
        }
    },
    methods: {
        toShopping: function () {
            window.location.href = '../product/index.html';
        },
        toOftenBuyPage: function () {
            window.location.href = '../oftenbuy/index.html';
        },
        goBack: function () {
            window.history.back(-1);
        },
        // 获得购物车区域高度
        getCartHeight: function () {
            this.cartHeight = document.body.clientHeight - document.getElementById('cart-products').offsetTop - 55;
        },
        // 设置推荐区域的高度
        getRecommendHeight: function () {
            this.recommendHeight = document.body.clientHeight - document.getElementById('recommend').offsetTop - 55;
        },
        getCardContentWidth: function () {
            this.cardContentWidth = document.body.clientWidth - 98;
        },
        toChangeControl: function () {
            this.control = (this.control == '编辑' ? '完成' : '编辑');
        },
        submitOrder: function () {
            store.set('order', {products: this.cartProducts, total: this.cartTotal});
            window.location.href = './order.html';
        },
        // 编辑商品-选中商品
        selectProduct: function (product) {
            if (!product.hasOwnProperty('isSelected')) {
                Vue.set( product, 'isSelected', false );
            }

            if (product.isSelected) {
                product.isSelected = false;
            } else {
                product.isSelected = true;
            }
        },
        // 编辑商品-全选
        selectAll: function () {
            this.isAll = !this.isAll;

            this.cartProducts.forEach((p, i, arr) => {
                if (!p.hasOwnProperty('isSelected')) {
                    Vue.set( p, 'isSelected', false );
                }

                p.isSelected = this.isAll;
            });
        },
        // 编辑商品-删除选中
        deleteSelected: function () {
            var productList = [],
                NObj = {};

            this.cartProducts.forEach((p, i, arr) => {
                if (!p.isSelected) {
                    productList.push(p);
                    NObj[p.id+'-'+p.nId] = p.num;
                }
            });

            this.cartProducts = productList;
            this.numObj = NObj;
            this.showRecommends();
        },
        touchPlus: function (p) {
            if (!p.hasOwnProperty('click')) {
                Vue.set(p, 'click', false);
            }
            p.click = true;
        },
        // 编辑商品-增加
        clickPlus: function (p) {
            if (!p.hasOwnProperty('showControl')) {
                Vue.set(p, 'showControl', true);
            }
            p.click = false;
            this.cartPlus(p);
        },
        /*// 编辑商品-减少
        reduce: function (p) {
            if (p.num > 1) {
                p.num--;
            }
        },*/
        // 加
        plus: function () {
            this.num++;
        },
        // 减
        reduce: function () {
            if (this.num>0) {
                this.num--;
            }
        },
        confirmProduct: function (NO) {
            this.product.num = NO;
            if (NO > 0 && this.cartProducts.indexOf(this.product) == -1) {
                this.cartProducts.push(this.product);
            }

            this.triggerRemoveProduct(NO, this.product);
        },
        // 取消
        cancel: function () {
            this.num = this.product.num;
            this.changeMask();
        },
        // 确定
        confirm: function () {
            /*this.product.num = this.num;
             if (this.cartProducts.indexOf(this.product) == -1) {
             this.cartProducts.push(this.product);
             }*/
            this.confirmProduct(this.num);
            this.numObj[this.product.id+'-'+this.product.nId] = this.num;
            // 本地缓存购物车数据
            // this.cacheCartProducts();
            this.changeMask();
        },
        // 隐藏 / 显示 遮罩层
        changeMask: function (p) {
            this.isShowMask = !this.isShowMask;
            if (this.isShowMask) {
                this.product = p;
                this.num = p.num;
            }
        },
        addProductToCart: function (p) {
            var n = p.normList[0];
            if (!p.hasOwnProperty('num')) {
                Vue.set(p, 'num', 1);
            }
            this.recommend = {};
            this.product = this.getProductFromPool(p, n);
            if(this.cartProducts.indexOf(this.product) == -1) {
                this.product.unitPrice = n.price;
                this.product.unit = n.unit;
                this.product.nId = n.id;
                this.cartProducts.push(this.product);
            } else {
                this.product.num++;
            }
        },
        touchProductToCart: function (p, event) {
            this.recommend = this.deepCopy(p);
            return false;
        },
        showRecommends: function () {
            if (this.cartProducts.length == 0) {
                this.getRecommends();
                this.control = '编辑';
            }
        }
    },
    mounted: function () {
        this.getCartProducts();
        this.getCartProductsTotal();
        this.showRecommends();
    }
});
window.onload = function () { // 页面加载完成事件
    var $div = document.getElementById('recommend'),// 获得推荐信息展示区域
        $cart = document.getElementById('cart-products'); // 购物车区域
    if($div != null) {
        Cart.getRecommendHeight(); // 设置推荐区域的高度
    }

    if ($cart != null) {
        Cart.getCartHeight();
        Cart.getCardContentWidth();
    }
}